//
//  ZLPopMenu.swift
//  ZLPopMenu
//
//  Created by 赵振磊 on 2025/1/11.
//

import UIKit
import SnapKit

public class ZLPopMenuCell: UICollectionViewCell {
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .boldSystemFont(ofSize: 14)
        label.textColor = .black
        label.textAlignment = .center
        return label
    }()
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    // MARK: - Initialization
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(titleLabel)
        contentView.addSubview(imageView)

        imageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(5)
            make.width.height.equalTo(20)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(imageView.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public func configure(with item: ZLPopMenuItem) {
        titleLabel.text = item.title
        titleLabel.font = item.font
        titleLabel.textColor = item.color
        if let image = item.image {
            imageView.isHidden = false
            imageView.image = image
            imageView.snp.updateConstraints { make in
                make.left.equalToSuperview().offset(5)
                make.width.height.equalTo(20)
            }
        } else {
            imageView.isHidden = true
            imageView.snp.updateConstraints { make in
                make.left.equalToSuperview().offset(0)
                make.width.height.equalTo(0)
            }
        }
    }
}

